/*  Utils.java Version 101 - 3rd April 2008
 *  Utils.java general utility functions
 *  See Versions for program version number
 *  Copyright (C) 2008  Andy Broomfield andy@newmediathinking.com
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

package net.blogbotplatform.blogbot;

import java.io.*;
import java.util.Calendar;
import java.util.Date;

public class Utils {
	/*a general class for doing general things*/
	
	//file vars
	private static BufferedReader in;
	private static String line = null;

	//timestamp vars
	public static int day;
	public static int month;
	public static int year;
	public static int hour;
	public static int minute;
	public static int second;
	public static long moment;
	
	public static String readConfigFile(String fileName) { 
	  System.out.println("start reading config file");
	  String configString = "";
	  
	  try {
	    //adjust the (relative) path to config.txt here
	    in = new BufferedReader(new FileReader(fileName));
	  } 
	  catch (IOException e) {
	    System.out.println("problem opening file" + e);
	  }

	  try {
	    for(;;) {
	      //read in a new line
	      line = in.readLine();
	      
	      if (line == null) break;
	      System.out.println("reading");
	      System.out.println(line);
	      configString += line + ",";
	    }
	  } 
	  catch (IOException e) {
	    ;
	  } 
	  finally {
	    try { 
	      in.close(); 
	    } 
	    catch (IOException e2) {
	      ;
	    }
	    System.out.println("finished reading config file");
	  }
	  
	  return configString;
	  //System.out.println(fileList);
	}

	public static void writeTextFile(String content, String fileName, String dirName) {
	  File dirObj = new File(dirName);

	  if (dirObj.exists() == false) {
	    System.err.println("Save Dir does not exist");
	    return;
	  }

	  try {
	    FileWriter out = new FileWriter(dirName + "/" + fileName, false);
	    out.write(content);
	    out.flush();
	    out.close();
	  }
	  catch( IOException e ) {
	    System.err.println(e);
	  }
	}

	public static int unsignedByteAsInt(int signedInt) {
			int unsignedInt;
			if (signedInt < 0) {
				unsignedInt = 256 + signedInt;
				} else {
				unsignedInt = signedInt;
				}
			//System.out.println(unsignedInt);
			return (unsignedInt);
			}

	public static void setTimeStamp() {
		/*sets a new timestamp which will be consitent with each place the time stamp is needed */
		Calendar rightNow = Calendar.getInstance();
		
		day = rightNow.get(Calendar.DAY_OF_MONTH);
		month = (rightNow.get(Calendar.MONTH) + 1);
		year = rightNow.get(Calendar.YEAR);
		hour = rightNow.get(Calendar.HOUR_OF_DAY);
		minute = rightNow.get(Calendar.MINUTE);
		second = rightNow.get(Calendar.SECOND);
		moment = System.currentTimeMillis();
	}
	
	public static String returnTimeStampAsText() { 
		String timeStampText; 
		timeStampText = "";
		if (day < 10) { timeStampText += "0" + day + "/"; } else { timeStampText += day + "/"; }
		if (month < 10) { timeStampText += "0" + month + "/"; } else { timeStampText += month + "/"; }
		timeStampText += year + " @ ";
		if (hour < 10) { timeStampText += "0" + hour + ":"; } else { timeStampText += hour + ":"; }
		if (minute < 10) { timeStampText += "0" + minute + "."; } else { timeStampText += minute + "."; }
		if (second < 10) { timeStampText += "0" + second; } else { timeStampText += second; }
		
		return(timeStampText);
	}
}
